package haierhome.krund.cn.daggertworetrofitrxjavamvp.rxjava.exampletwo;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;

import haierhome.krund.cn.daggertworetrofitrxjavamvp.rxjava.exampletwo.exception.ApiException;
import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;

/**
 * Created by Administrator on 2018/8/14.
 */

class BaseSubscriber<T> extends Subscriber<T> {

    protected Context mContext;

    public BaseSubscriber(Context context) {
        this.mContext = context;
    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(final Throwable e) {
        Log.w("Subscriber onError", e);
        if (e instanceof HttpException) {
            // We had non-2XX http error
            Toast.makeText(mContext, "服务器错误", Toast.LENGTH_SHORT).show();
        } else if (e instanceof IOException) {
            // A network or conversion error happened
            Toast.makeText(mContext, "无法连接到服务器", Toast.LENGTH_SHORT).show();
        } else if (e instanceof ApiException) {
            ApiException exception = (ApiException) e;
            if (exception.isTokenExpried()) {
                //处理token失效对应的逻辑
            } else {
                Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onNext(T t) {

    }
}
